from pysqlite2 import dbapi2 as sqlite

class db:
  def __init__(self, dbname):
    self.con = sqlite.connect(dbname)

  def __del__(self):
    self.con.close()

  def commit(self):
    self.con.commit()

  def createtable(self, schema):
    self.con.execute('create table ' + schema)

  def createtables(self, schemas):
    for schema in schemas:
      self.createtable(schema)
  
  def createindex(self, index):
    self.con.execute('create index ' + index)

  def createindices(self, indices):
    for index in indices:
      self.createindex(index)

  def droptable(self, table):
    self.con.execute('drop table if exists ' + table)

  def queryone(self, table, fields, condition):
    q = 'select ' + fields + ' from ' + table + ' where ' + condition
    return self.con.execute(q).fetchone()

  def query(self, table, fields, condition, distinct = ''):
    q = 'select ' + distinct + ' ' + fields + ' from ' + table + ' where ' + condition
    return self.con.execute(q)

  def queryall(self, table, fields, order = ''):
    return self.con.execute('select ' + fields + ' from ' + table + ' ' + order)

  def insert(self, table, fields, values):
    q = 'insert into ' + table + ' (' + fields + ') values (' + values + ')'
    return self.con.execute(q)

  def insertfromtable(self, table, fields, fromtable):
    self.con.execute('insert into ' + table + ' select ' + fields + ' from ' + fromtable)

  def update(self, table, assignments, condition):
    q = 'update ' + table + ' set ' + assignments + ' where ' + condition
    self.con.execute(q)



